package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.Order;
import com.burgeon.backend.SHOP.entity.OrderResult;
import com.burgeon.backend.SHOP.entity.PayData;
import com.burgeon.backend.SHOP.mappers.GoodsMapper;
import com.burgeon.backend.SHOP.service.OrderService;
import com.burgeon.backend.SHOP.service.ex.OrderExceptionApp;
import com.burgeon.backend.SHOPBACKEND.rest.GetGoodsCode;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RequestMapping("shop/order")
@RestController
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    //秒杀自提
    @RequestMapping("seckillorderZT")
    public ResponseResult<PayData> seckillorderZT(String sid, String phoneZT,String nameZT, String code, String color,
                                                   String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                                   String emp,String dateZT,String store,String distance, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);

        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = orderService.seckillCreateOrderZT(uid,openid,phoneZT,nameZT,sid,code,store,
                distance,dateZT,color + "-" + size,phone,token,emp,comment);
        return new ResponseResult<PayData>(SUCCESS, data);
    }


    @RequestMapping("ztdelay")
    public ResponseResult<Integer> NumberOfDaysOfSelfPickupDelay(HttpSession session) {
        String token = session.getAttribute("token").toString();
        Integer data = orderService.NumberOfDaysOfSelfPickupDelay(token);
        return new ResponseResult<Integer>(SUCCESS,data);
    }
    //取消订单
    @RequestMapping("del")
    public ResponseResult<Void> delOrder(String id, HttpSession session) {
        String token = session.getAttribute("token").toString();
        orderService.DelOrder(id, token);
        return new ResponseResult<>(SUCCESS);
    }

    //点击收货
    @RequestMapping("sh")
    public ResponseResult<Void> UpOrderState(String id, HttpSession session) {
        String token = session.getAttribute("token").toString();
        orderService.UpOrderState(id, token);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 立即付款 传入订单id
     *
     * @param oid
     * @param session
     * @return
     */
    @RequestMapping("payment")
    public ResponseResult<PayData> PaymentOrder(String oid, HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        PayData data = orderService.payForTheOrder(oid, openid, token);
        return new ResponseResult<PayData>(SUCCESS, data);
    }

    /**
     * 支付结果
     *
     * @param session
     * @return
     */
    @RequestMapping("result")
    public ResponseResult<OrderResult> FindOrder(String oid, HttpSession session) {
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        OrderResult data = orderService.GetLatestOrder(oid, uid, token);
        return new ResponseResult<OrderResult>(SUCCESS, data);
    }

    @RequestMapping("findorder")
    public ResponseResult<Order> FindOrder(String id) {
        Order data = orderService.FindOrder(id);
        return new ResponseResult<Order>(SUCCESS, data);
    }

    @RequestMapping("getdata")
    public ResponseResult<List<Order>> GetUserOrder(HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        List<Order> data = orderService.getUserOrder(uid, token);

        return new ResponseResult<List<Order>>(SUCCESS, data);
    }

    /**
     * 获取订单数量
     *
     * @param session
     * @return
     */
    @RequestMapping("ordercount")
    public ResponseResult<HashMap> GetOrderCount(HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        System.err.println("uid="+uid+","+token);

        HashMap data = orderService.getWebOrderCount(uid, token);

        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    //生日积分自提
    @RequestMapping("integralOrderBirthdayZT")
    public ResponseResult<PayData> integralOrderBirthdayZT(String gid, String phoneZT,String nameZT, String code, String color,
                                                           String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                                           String emp,String dateZT,String store,String distance, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);

        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = orderService.integralOrderBirthdayZT(uid,openid,phoneZT,nameZT,gid,code,store,
                distance,dateZT,color + "-" + size,phone,token,emp,comment);
        return new ResponseResult<PayData>(SUCCESS, data);
    }

    //生日好礼积分快递订单
    @RequestMapping("integralBirthdayOrder")
    public ResponseResult<PayData> integralBirthdayOrder(String gid, String aid, String mid, String code, String color,
                                                         String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                                         String emp, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);

        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = orderService.integralBirthdayOrder(uid, openid, aid, mid, gid, code,
                color + "-" + size, phone, token, comment, emp);
        return new ResponseResult<PayData>(SUCCESS, data);
    }

    //积分自提
    @RequestMapping("integralorderZT")
    public ResponseResult<PayData> integralOrderZT(String gid, String phoneZT,String nameZT, String code, String color,
                                                 String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                                 String emp,String dateZT,String store,String distance, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);

        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = orderService.integralCreateOrderZT(uid,openid,phoneZT,nameZT,gid,code,store,
                distance,dateZT,color + "-" + size,phone,token,emp,comment);
        return new ResponseResult<PayData>(SUCCESS, data);
    }

//积分
    @RequestMapping("integralorder")
    public ResponseResult<PayData> integralOrder(String gid, String aid, String mid, String code, String color,
                                                 String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                                 String emp, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);

        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = orderService.integralCreateOrder(uid, openid, aid, mid, gid, code,
                color + "-" + size, phone, token, comment, emp);
        return new ResponseResult<PayData>(SUCCESS, data);
    }

    /**
     *
     * @param gid 商品id
     * @param aid 地址id
     * @param mid 快递id
     * @param cid 优惠券id
     * @param code 款号
     * @param color 颜色
     * @param size 尺寸
     * @param comment 备注
     * @param emp emp 可空
     * @param session
     * @return
     */
    @RequestMapping("goodsorder")
    public ResponseResult<PayData> GoodsOrder(String gid, String aid, String mid, String cid, String code, String color,
                                              String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                              String emp, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        String isPlus;  //是plus session会赋值Y  不是为null
        if (null != session.getAttribute("user_is_plus")) {
            isPlus = session.getAttribute("user_is_plus").toString();
        } else {
            isPlus = "N";
        }


        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = orderService.createOrder(uid, openid, aid, mid, gid, cid, code,
                color + "-" + size, phone, token, comment, emp, discount, isPlus);
        return new ResponseResult<PayData>(SUCCESS, data);
    }

    @RequestMapping("basketorder")
    public ResponseResult<PayData> BasketOrder(String aid, String mid, String cid, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment, String emp, HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        String isPlus;  //是plus session会赋值Y  不是为null
        if (null != session.getAttribute("user_is_plus")) {
            isPlus = session.getAttribute("user_is_plus").toString();
        } else {
            isPlus = "N";
        }

        PayData data = orderService.createOrderByBasket(uid, openid, aid, mid, cid, phone,
                token, comment, emp, discount, isPlus);

        return new ResponseResult<PayData>(SUCCESS, data);
    }

    @RequestMapping("seckillorder")
    public ResponseResult<PayData> seckillCreateOrder(String sid, String aid, String mid,
                                                      String cid, String code, String color,
                                                      String size, @RequestParam(value = "comment", required = false, defaultValue = " ")
                                                              String comment, String emp, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }




        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);

        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = orderService.seckillCreateOrder(uid, openid, aid, mid, sid, cid, code, color + "-" + size, phone, token, comment, emp);
        return new ResponseResult<PayData>(SUCCESS, data);
    }

}
